package com.leetcode.双周赛.第49场;

/**
 * @author: xiaomi
 * @date: 2021/4/3
 * @description: 5706. 句子相似性 III
 * https://leetcode-cn.com/contest/biweekly-contest-49/problems/sentence-similarity-iii/
 */
public class B_5706_句子相似性_III {

    static B_5706_句子相似性_III action = new B_5706_句子相似性_III();

    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
        test5();
    }

    static void test1() {
        //true
        String s1 = "My name is Haley";
        String s2 = "My Haley";
        boolean res = action.areSentencesSimilar(s1, s2);
        System.out.println("res = " + res);
    }

    static void test2() {
        //false
        String s1 = "of";
        String s2 = "A lot of words";
        boolean res = action.areSentencesSimilar(s1, s2);
        System.out.println("res = " + res);
    }

    static void test3() {
        //true
        String s1 = "Eating right now";
        String s2 = "Eating";
        boolean res = action.areSentencesSimilar(s1, s2);
        System.out.println("res = " + res);
    }

    static void test4() {
        //false
        String s1 = "Luky";
        String s2 = "Lucccky";
        boolean res = action.areSentencesSimilar(s1, s2);
        System.out.println("res = " + res);
    }

    static void test5() {
        //true
        String s1 = "qbaVXO Msgr aEWD v ekcb";
        String s2 = "Msgr aEWD ekcb";
        boolean res = action.areSentencesSimilar(s1, s2);
        System.out.println("res = " + res);
    }

    /**
     * 由一些单词与它们之间的[单个空格]组成，且[句子的开头和结尾没有多余空格]
     * 每个单词都 只 包含大写和小写英文字母
     * --
     * 大串必定包含小串
     *
     * @param sentence1
     * @param sentence2
     * @return
     */
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        //需要一个标志位：0-初始，还木有句子；1-已经有一个句子了
        int label = 0;
        String[] splits1 = sentence1.split(" ");
        String[] splits2 = sentence2.split(" ");
        int index1 = 0, index2 = 0;
        int count = 0;
        int len1 = splits1.length;
        int len2 = splits2.length;
        int endIndex1 = len1 - 1, endIndex2 = len2 - 1;
        boolean len1Longer = len1 > len2;
        if (len1Longer) {
            // s1 大于 s2
            while (true) {
                if (splits1[index1].equals(splits2[index2])) {
                    if (label == 2) {
                        count++;
                        if (count == 2) {
                            return false;
                        }
                    }
                    label = 1;
                    if (index2 == endIndex2) {
                        if (index1 != endIndex1) {
                            if (count == 0) {
                                return true;
                            }
                            return false;
                        } else {
                            return true;
                        }
                    }
                    if (index1 == endIndex1) {
                        return false;
                    }
                    index1++;
                    index2++;
                } else {
                    label = 2;
                    index1++;
                    if (index1 == len1) {
                        return false;
                    }
                }
            }

        } else {
            while (true) {
                if (splits1[index1].equals(splits2[index2])) {
                    if (label == 2) {
                        count++;
                        if (count == 2) {
                            return false;
                        }
                    }
                    label = 1;
                    if (index1 == endIndex1) {
                        if (index2 != endIndex2) {
                            if (count == 0) {
                                return true;
                            }
                            return false;
                        } else {
                            return true;
                        }
                    }
                    if (index2 == endIndex2) {
                        return false;
                    }
                    index1++;
                    index2++;
                } else {
                    label = 2;
                    index2++;
                    if (index2 == len2) {
                        return false;
                    }
                }
            }
        }
    }
}
